package com.sxpi.user.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @Author: 董伟豪
 * @Date: 2022/11/12 11:07
 * redission的初始化
 */
@Configuration
public class RedissonConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        // 主从复制的版本
        //config.useMasterSlaveServers().addSlaveAddress("redis://127.0.0.1:6379");
        // 哨兵
        //config.useSentinelServers().addSentinelAddress("redis://127.0.0.1:6379");
        // 集群
        //config.useClusterServers()
        //        .addNodeAddress("redis://127.0.0.1:6379","redis://127.0.0.1:6380");
        // 单节点
        config
                // 多路复用，windows可能没有什么用
                .setTransportMode(TransportMode.NIO)
                // 看门狗机制
                .setLockWatchdogTimeout(30 * 1000)
                .useSingleServer()
                .setDatabase(2)
                .setAddress(environment.getProperty("redisson.host.config"))
                .setKeepAlive(true);
        // 创建客户端
        return Redisson.create(config);
    }

}
